<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/layouts/taglib.jsp" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>版本信息</title>
		<%@include file="/WEB-INF/layouts/index-header.jsp"%>
	</head>
	<body>

		<div class="tpanel">
			<div class="panel-content">
				<div class="container-fluid">
					<div id="versionList" style="background: cornsilk;display:none;padding:10px;margin:10px;border:1px solid #ccc;">

					</div>
					<div id="content" style="padding:50px;">

					</div>
				</div>
			</div>
		</div>

		<script type="text/javascript" src="${staticPath}/admin/markdown/markd.min.js"></script>
		<script type="text/javascript" src="${staticPath}/admin/markdown/highlight.min.js"></script>
		<script type="text/javascript">
			changeTitle('版本信息');

			$(function() {
                $.get('${ctx}/version.md?timestamp=' + new Date().getTime(), function(txt) {
                    $('#content').html(marked(txt));
				});
                $.get('${ctx}/admin/check-version', function(r) {
                    if(r.code == 'OK') {
						var respCheckVersion = r.body;
						if(respCheckVersion.needUpgrade) {
						    $('#versionList').show();
						    var html = '';
						    html += '<h5>' + respCheckVersion.serverAppName + ' 检测到可升级版本！</h5>';
						    html += '<ul>';
                            var versionList = respCheckVersion.versionList;
						    for(var i in versionList) {
						        html += '<li style="margin-bottom:5px;">';
                                html += '<div>版本号：<b>' + versionList[i].name + '</b> <a href="javascript:upgrade(\'' + versionList[i].name + '\', \'' + versionList[i].url + '\')"> 立即升级</a></div>';
                                html += '<div>发布时间：' + versionList[i].createdTime + '</div>';
                                html += '<div>升级内容：' + versionList[i].detail + '</div>';
                                html += '</li>';
							}
                            html += '</div>';
						    $('#versionList').html(html);
						}
					}else {
                        window.parent.layer.msg('检查版本失败:' + resp.message, {icon: 2});
					}
                }, 'json');
			});

			function upgrade(version, url) {
                window.parent.bootbox.prompt({
                    title: '点击升级操作后将无法撤销，请输入密码确认',
                    onEscape : true,
                    backdrop : true,
                    animate : true,
                    buttons: {
                        confirm: {
                            label: '升级',
                            className: 'btn-danger'
                        },
                        cancel: {
                            label: '取消',
                            className: ''
                        }
                    },
                    inputType: 'password',
                    callback: function (result) {
                        if(result) {
                            $.post('${ctx}/admin/upgrade',{version:version, url: url,password:result},function(resp) {
                                if(resp.code == 'OK') {
                                    window.parent.layer.msg('正在升级', {icon: 1});
                                }else {
                                    window.parent.layer.msg('升级失败:' + resp.message, {icon: 2});
                                }
                            },'json');
                        }
                    }
                });
			}

			function wsHandler() {
			    console.log('wsHAndle555')
			}
		</script>
	</body>
</html>
